<!DOCTYPE html> 
<html>
  <head>
    <!--META-->
    <meta charset="utf-8">
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
    <meta name="msapplication-tap-highlight" content="no" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">

    <!--CSS-->
    <link rel="stylesheet" href="API/css/themes/default/jquery.mobile.min.css" />
    <link rel="stylesheet" href="API/css/mycss.css" />
    <link rel="stylesheet" href="css/mycss.css" />

    <!--JS-->
    <script type="text/javascript" src="include/config.js"></script>
    <script type="text/javascript" src="API/js/jquery.min.js"></script>
    <script type="text/javascript" src="API/js/jquery.mobile.min.js"></script>
    <script type="text/javascript" src="API/js/functionsLib.js"></script>
    <script type="text/javascript" src="js/fonctions.js"></script>

    <script type="text/javascript">
    ///////////////////
    //BLOCK VARIABLE GLOBAL
    ///////////////////
    var id_page = 22;
    var g_id_tournoi;
    var g_couleurFontBon = "#04B404";
    var g_couleurFontMauvais = "#FF0000";
    var g_couleurFontDefaut = "#333333";
    var g_couleurFontTips = "#a1a1a1";
    
    var g_timer;
    var g_horloge;
    
    var g_decompte = 20;
    var g_etatTimer = 'init'; //init, encours, pause, stop
    var g_round = 1;

    ///////////////////
    //BLOCK FONCTIONS EVENEMENTS
    ///////////////////
    //Fin chargement page
    function OnLoad() {
        try {
            g_id_tournoi = $.functionsLib.getParamGet("id_tournoi");
            
            chargerTitre(g_id_tournoi);
            
            chargerInfos();
            
            chargerParticipants();
            
            chargerDecompte();
            
            chargerHorloge();
        }
        catch (er) {
            $.functionsLib.log(0, "ERROR(OnLoad):" + er.message);
        }
    }

    ///////////////////
    //BLOCK FONCTIONS METIER
    ///////////////////
    /**
     * chargerParticipants
     */
    function chargerParticipants(){
        try {
            var tabInput = { id_tournoi : g_id_tournoi };
            var json_retour = $.functionsLib.callRest(domaine+"phpsql/mysql_getParticipantsTournoi.php", {}, tabInput);
            var dataRetour = json_retour["data"]["resultat"]["data"];
            for(var indice in dataRetour){
                var strHtmlDisable = "";
                if(dataRetour[indice]["fin"] != "0000-00-00 00:00:00"){
                    strHtmlDisable = " class=\"ui-disabled\"";
                }
                
                var strHtml = "";
                strHtml += "<div data-role=\"controlgroup\" data-type=\"horizontal\" data-mini=\"true\">";
                strHtml += "<a href=\"#\" data-role=\"button\" data-theme=\"e\" "+strHtmlDisable+">"+dataRetour[indice]["prenom"]+"."+dataRetour[indice]["nom"].substring(0,1)+"</a>";
                strHtml += "<a href=\"javascript:sortirParticipant('"+dataRetour[indice]["id"]+"');\" data-role=\"button\" data-iconpos=\"notext\" data-icon=\"delete\" data-theme=\"e\" "+strHtmlDisable+">Sortir</a>";
                strHtml += "</div>";
                strHtml += "<div data-role=\"controlgroup\" data-type=\"horizontal\" data-mini=\"true\">";
                strHtml += "<a href=\"#\" data-role=\"button\" data-theme=\"e\" "+strHtmlDisable+">"+dataRetour[indice]["nb_tapis"]+"</a>";
                strHtml += "<a href=\"javascript:recaveParticipant('"+dataRetour[indice]["id"]+"');\" data-role=\"button\" data-iconpos=\"notext\" data-icon=\"refresh\" data-theme=\"e\" "+strHtmlDisable+">recave</a>";
                strHtml += "<a href=\"javascript:addonParticipant('"+dataRetour[indice]["id"]+"');\" data-role=\"button\" data-iconpos=\"notext\" data-icon=\"plus\" data-theme=\"e\" "+strHtmlDisable+">add-on</a>";
                strHtml += "</div>";
                $('#place'+(parseInt(indice)+1)).html(strHtml).trigger('create');
            }   
        }
        catch (er) {
            $.functionsLib.log(0, "ERROR(chargerParticipants):" + er.message);
        }
    }
    
    /**
     * chargerInfos
     */
    function chargerInfos(){
        try {
            var tabInput = { id_tournoi : g_id_tournoi };
            var json_retour = $.functionsLib.callRest(domaine+"phpsql/mysql_getInfosLive.php", {}, tabInput);
            
            var dateTimeDepart = json_retour["data"]["strSebutDecompte"];
            var intRestantSeconde = parseInt(json_retour["data"]["strTempsRestantSeconde"]);
            var intDecompte = parseInt(json_retour["data"]["strDecompte"]);
            g_etatTimer = json_retour["data"]["strEtatDecompte"];
            if(g_etatTimer == "encours"){
                g_decompte = intDecompte;
            }else{
                g_decompte = intRestantSeconde;
            }
            g_round = parseInt(json_retour["data"]["strRoundEnCours"]);
            
            var intNbJetonsTapis = 0;
            intNbJetonsTapis = parseInt(json_retour["data"]["strNbJetonsTapis"]);
            
            var intNbTapis = 0;
            intNbTapis = parseInt(json_retour["data"]["strNbTapis"]);
            
            var intNbParticpantsActif = 0;
            intNbParticpantsActif = parseInt(json_retour["data"]["strNbParticipant"]);
            
            var intTapisMoyen = 0;
            intTapisMoyen = Math.round((intNbJetonsTapis*intNbTapis)/intNbParticpantsActif);
            
            var intValeurTapis = 0;
            intValeurTapis = parseInt(json_retour["data"]["strValeurTapis"]);
            
            var int50 = 0;
            int50 = Math.round((intValeurTapis*intNbTapis)*0.5);
            
            var int30 = 0;
            int30 = Math.round((intValeurTapis*intNbTapis)*0.3);
            
            var int20 = 0;
            int20 = Math.round((intValeurTapis*intNbTapis)*0.2);
            
            var dateDebut = "";
            dateDebut = json_retour["data"]["strDateDebut"];
            
            var strHtml = "";
            strHtml = "NBT : "+intNbTapis;
            $('#labelNbtapis').html(strHtml).trigger('create');
            
            var strHtml = "";
            strHtml = "TM : "+intTapisMoyen+" jetons";
            $('#labelTM').html(strHtml).trigger('create');
             
            var strHtml = "";
            strHtml = "TM : "+intTapisMoyen+" jetons";
            $('#labelTM').html(strHtml).trigger('create'); 
            
            var strHtml = "";
            strHtml = "50% : "+int50+"€";
            $('#label50').html(strHtml).trigger('create'); 
            
            var strHtml = "";
            strHtml = "30% : "+int30+"€";
            $('#label30').html(strHtml).trigger('create'); 
            
            var strHtml = "";
            strHtml = "20% : "+int20+"€";
            $('#label20').html(strHtml).trigger('create'); 
            
            $('#labelSmallBlind').html(json_retour["data"]["strSmall_blind"]).trigger('create'); 
            
            $('#labelBigBlind').html(json_retour["data"]["strBig_blind"]).trigger('create'); 
            
            var strAnte = json_retour["data"]["strAnte"];
            if(strAnte == "0"){
                strAnte = "-";
            }
            $('#labelAnte').html(strAnte).trigger('create'); 
            
            var strHtml = "";
            if(dateDebut != "0000-00-00 00:00:00"){
                strHtml = "<div data-role=\"controlgroup\" data-type=\"horizontal\" data-theme=\"a\">";
                strHtml += "<a href=\"#\" data-role=\"button\" data-theme=\"a\">D&eacute;but : "+dateDebut+"</a>";
                strHtml += "</div>";
            }
            $('#labelDateTournoi').html(strHtml).trigger('create'); 
        }
        catch (er) {
            $.functionsLib.log(0, "ERROR(chargerInfos):" + er.message);
        }
    }
    
    /**
     * sortirParticipant
     * 
     * @param {array} p_id_user
     */
    function sortirParticipant(p_id_user){
        try {
            var tabInput = { id_user : p_id_user, id_tournoi : g_id_tournoi };
             var json_retour = $.functionsLib.callRest(domaine+"phpsql/mysql_outParticipant.php", {}, tabInput);
            if(json_retour["strErreur"] == ""){
                chargerParticipants();
                chargerInfos();
            }else{
                $.functionsLib.notification("Erreur sortirParticipant : "+json_retour["strErreur"],$.functionsLib.oda_msg_color.ERROR);
            }
        }
        catch (er) {
            $.functionsLib.log(0, "ERROR(sortirParticipant):" + er.message);
        }
    }
    
    /**
     * recaveParticipant
     * 
     * @param {array} p_id_user
     */
    function recaveParticipant(p_id_user){
        try {
            var tabInput = { id_user : p_id_user, id_tournoi : g_id_tournoi, type : 'recave' };
            var json_retour = $.functionsLib.callRest(domaine+"phpsql/mysql_tapisParticipant.php", {}, tabInput);
            if(json_retour["strErreur"] == ""){
                chargerParticipants();
                chargerInfos();
            }else{
                $.functionsLib.notification("Erreur recaveParticipant : "+json_retour["strErreur"],$.functionsLib.oda_msg_color.ERROR);
            }
        }
        catch (er) {
            $.functionsLib.log(0, "ERROR(recaveParticipant):" + er.message);
        }
    }
    
    /**
     * addonParticipant
     * 
     * @param {array} p_id_user
     */
    function addonParticipant(p_id_user){
        try {
            var tabInput = { id_user : p_id_user, id_tournoi : g_id_tournoi, type : 'addon' };
            var json_retour = $.functionsLib.callRest(domaine+"phpsql/mysql_tapisParticipant.php", {}, tabInput);
            if(json_retour["strErreur"] == ""){
                chargerParticipants();
                chargerInfos();
            }else{
                $.functionsLib.notification("Erreur recaveParticipant : "+json_retour["strErreur"],$.functionsLib.oda_msg_color.ERROR);
            }
        }
        catch (er) {
            $.functionsLib.log(0, "ERROR(addonParticipant):" + er.message);
        }
    }
      
    /**
     * chargerDecompte
     */
    function chargerDecompte(){
        try {
            if((g_etatTimer == "encours")&&(g_decompte > 0)){
                g_timer = setInterval('CompteaRebour()',1000);
            }
            afficheDecompte();
        }
        catch (er) {
            $.functionsLib.log(0, "ERROR(chargerDecompte):" + er.message);
        }
    }
    
    /**
     * CompteaRebour
     */
    function CompteaRebour(){
        try {
            g_decompte -= 1;
            
            $("#labelDecompte").html("Tour:"+g_round+" - "+$.functionsLib.getStrDate.convertSecondsToTime(g_decompte).substring(3));
            
            if(g_decompte == 0){
                clearInterval(g_timer);
                var tabInput = { id_tournoi : g_id_tournoi, action : 'upRound' };
                var json_retour = $.functionsLib.callRest(domaine+"phpsql/mysql_actionDecompte.php", {}, tabInput);
                if(json_retour["strErreur"] == ""){
                    chargerInfos();
                    afficheDecompte();
                    g_timer = setInterval('CompteaRebour()',1000);
                }else{
                    $.functionsLib.notification("Erreur CompteaRebour : "+json_retour["strErreur"],$.functionsLib.oda_msg_color.ERROR);
                }
            }
        }
        catch (er) {
            $.functionsLib.log(0, "ERROR(chargerDecompte):" + er.message);
        }
    }
    
    /**
     * actionDecompte
     */
    function actionDecompte(p_action){
        try {
            switch (p_action) { 
                case 'start': 
                    var tabInput = { id_tournoi : g_id_tournoi, action : 'start' };
                    var json_retour = $.functionsLib.callRest(domaine+"phpsql/mysql_actionDecompte.php", {}, tabInput);
                    if(json_retour["strErreur"] == ""){
                        chargerInfos();
                        afficheDecompte();
                        g_timer = setInterval('CompteaRebour()',1000);
                    }else{
                        $.functionsLib.notification("Erreur actionDecompte : "+json_retour["strErreur"],$.functionsLib.oda_msg_color.ERROR);
                    }
                    break; 
                case 'pause': 
                    var tabInput = { id_tournoi : g_id_tournoi, action : 'pause' };
                    var json_retour = $.functionsLib.callRest(domaine+"phpsql/mysql_actionDecompte.php", {}, tabInput);
                    if(json_retour["strErreur"] == ""){
                        chargerInfos();
                        afficheDecompte();
                        clearInterval(g_timer);
                    }else{
                        $.functionsLib.notification("Erreur actionDecompte : "+json_retour["strErreur"],$.functionsLib.oda_msg_color.ERROR);
                    }
                    break; 
                case 'stop': 
                    var tabInput = { id_tournoi : g_id_tournoi, action : 'stop' };
                    var json_retour = $.functionsLib.callRest(domaine+"phpsql/mysql_actionDecompte.php", {}, tabInput);
                    if(json_retour["strErreur"] == ""){
                        chargerInfos();
                        afficheDecompte();
                        clearInterval(g_timer);
                    }else{
                        $.functionsLib.notification("Erreur actionDecompte : "+json_retour["strErreur"],$.functionsLib.oda_msg_color.ERROR);
                    }
                    break; 
            }
        }
        catch (er) {
            $.functionsLib.log(0, "ERROR(actionDecompte):" + er.message);
        }
    }
    
    /**
     * chargerHorloge
     */
    function chargerHorloge(){
        try {
            var strHtml = "";
            strHtml += $.functionsLib.getStrDate.getStrDateTime();
            
            $('#labelHeure').html(strHtml).trigger('create'); 
            
            g_horloge = setInterval('vueHorloge()',1000);
        }
        catch (er) {
            $.functionsLib.log(0, "ERROR(chargerHorloge):" + er.message);
        }
    }
    
    ///////////////////
    //BLOCK FONCTIONS AFFICHAGE
    ///////////////////
    /**
     * afficheDecompte
     */
    function afficheDecompte(){
        try {
            var strHtml = "";
            strHtml += "<div data-role=\"controlgroup\" data-type=\"horizontal\" data-theme=\"a\">";
            if((g_etatTimer == "encours")&&(g_decompte > 0)){
                strHtml += "<a href=\"#\" data-role=\"button\" data-theme=\"a\"><label id=\"labelDecompte\">Tour:"+g_round+" - "+$.functionsLib.getStrDate.convertSecondsToTime(g_decompte).substring(3)+"</label></a>";
                strHtml += "<a href=\"javascript:actionDecompte('pause');\" data-role=\"button\" data-iconpos=\"notext\" data-icon=\"arrow-u\" data-theme=\"a\"></a>"; 
                strHtml += "<a href=\"javascript:actionDecompte('stop');\" data-role=\"button\" data-iconpos=\"notext\" data-icon=\"delete\" data-theme=\"a\"></a>";
            }else if(g_etatTimer != "stop"){
                if(g_etatTimer == "pause"){
                    strHtml += "<a href=\"#\" data-role=\"button\" data-theme=\"a\"><label id=\"labelDecompte\">Tour:"+g_round+" - "+$.functionsLib.getStrDate.convertSecondsToTime(g_decompte).substring(3)+"(P)</label></a>";
                }else{
                    strHtml += "<a href=\"#\" data-role=\"button\" data-theme=\"a\"><label id=\"labelDecompte\">Tour:"+g_round+" - "+$.functionsLib.getStrDate.convertSecondsToTime(g_decompte).substring(3)+"</label></a>";
                }
                strHtml += "<a href=\"javascript:actionDecompte('start');\" data-role=\"button\" data-iconpos=\"notext\" data-icon=\"arrow-r\" data-theme=\"a\"></a>"; 
                strHtml += "<a href=\"javascript:actionDecompte('stop');\" data-role=\"button\" data-iconpos=\"notext\" data-icon=\"delete\" data-theme=\"a\"></a>";
            }else{
                strHtml += "<a href=\"#\" data-role=\"button\" data-theme=\"a\"><label id=\"labelDecompte\">Tour:"+g_round+" - Stop</label></a>";
            }
            strHtml += "</div>";
            
             $('#divDecompte').html(strHtml).trigger('create'); 
        }
        catch (er) {
            $.functionsLib.log(0, "ERROR(afficheDecompte):" + er.message);
        }
    }
    
    /**
     * vueHorloge
     */
    function vueHorloge(){
        try {
            var strHtml = "";
            strHtml += $.functionsLib.getStrDate.getStrDateTime();
            
            $('#labelHeure').html(strHtml).trigger('create'); 
        }
        catch (er) {
            $.functionsLib.log(0, "ERROR(vueHorloge):" + er.message);
        }
    }
    
</script>

</head>
<body onload="OnLoad();">

    <!-- page -->
    <div data-role="page" data-title="Titre">

        <!-- /panel -->
        <div data-role="panel" id="mypanel" data-display="overlay" data-position="left">

        </div>
        <!-- /panel -->

        <!-- header -->
        <div data-role="header" data-position="fixed">
            <a href="#mypanel" data-role="button" data-icon="home" data-iconpos="notext">home</a>
            <h1 id="id_titre">titre</h1>
            <a href="javascript:window.location = ('./api_page_contact.html?mili='+$.functionsLib.getMilise());" data-role="button" data-icon="info" data-iconpos="notext">Contact</a>
        </div>
        <!-- /header -->
         
        <!-- navbar -->
        <div data-role="navbar" data-grid="c">
            <ul>
                <li><a href="javascript:window.location = ('./page_tournoi_param.html?mili='+$.functionsLib.getMilise()+'&id_tournoi='+getIdtournoi());">Param&egrave;trage</a></li>
                <li><a href="#" class="ui-btn-active ui-state-persist">Live</a></li>
                <li><a href="javascript:window.location = ('./page_tournoi_histo.html?mili='+$.functionsLib.getMilise()+'&id_tournoi='+getIdtournoi());">Historique</a></li>
                <li><a href="javascript:window.location = ('./page_tournoi_rules.html?mili='+$.functionsLib.getMilise()+'&id_tournoi='+getIdtournoi());">R&egrave;gles</a></li>
            </ul>
        </div>
        <!-- /navbar -->

        <!-- content -->
        <div data-role="content" id="main_content">
            <div data-role="collapsible" data-collapsed="false" data-mini="true">
                <h4>Live</h4>
                <center>
                <table style="width: 960px;height: 531px;background-image: url('img/Table-final.png');text-align: center" >
                    <tbody>
                    <tr>
                      <td style="width: 160px;height: 106px"></td>
                      <td style="width: 160px;height: 106px"></td>
                      <td style="width: 160px;height: 106px"></td>
                      <td style="width: 160px;height: 106px"></td>
                      <td style="width: 160px;height: 106px"></td>
                      <td style="width: 160px;height: 106px"></td>
                    </tr>
                    <tr>
                      <td style="width: 160px;height: 106px"></td>
                      <td style="width: 160px;height: 106px;text-align: left"><label id="place4"></label></td>
                      <td style="width: 160px;height: 106px"><label id="place5"></label></td>
                      <td style="width: 160px;height: 106px"><label id="place6"></label></td>
                      <td style="width: 160px;height: 106px;text-align: right"><label id="place7"></label></td>
                      <td style="width: 160px;height: 106px"></td>
                    </tr>
                    <tr>
                      <td style="width: 160px;height: 106px"><label id="place3"></label></td>
                      <td style="width: 160px;height: 106px"></td>
                      <td colspan="2" style="width: 320px;height: 106px">
                        <div id="divDecompte"></div>
                        <div data-role="controlgroup" data-type="horizontal" data-theme="a">
                            <a href="#" data-role="button" data-theme="a">SB:<label id="labelSmallBlind">N.A.</label></a>
                            <a href="#" data-role="button" data-theme="a">A:<label id="labelAnte">N.A.</label></a>
                            <a href="#" data-role="button" data-theme="a">BB:<label id="labelBigBlind">N.A.</label></a>
                        </div>
                      </td>
                      <td style="width: 160px;height: 106px"></td>
                      <td style="width: 160px;height: 106px"><label id="place8"></label></td>
                    </tr>
                    <tr>
                      <td style="width: 160px;height: 106px"></td>
                      <td style="width: 160px;height: 106px;text-align: left"><label id="place2"></label></td>
                      <td style="width: 160px;height: 106px"><label id="place1"></label></td>
                      <td style="width: 160px;height: 106px"><label id="place10"></label></td>
                      <td style="width: 160px;height: 106px;text-align: right"><label id="place9"></label></td>
                      <td style="width: 160px;height: 106px"></td>
                    </tr>
                    <tr>
                      <td style="width: 160px;height: 106px"></td>
                      <td style="width: 160px;height: 106px"></td>
                      <td style="width: 160px;height: 106px"></td>
                      <td style="width: 160px;height: 106px"></td>
                      <td style="width: 160px;height: 106px"></td>
                      <td style="width: 160px;height: 106px"></td>
                    </tr>
                  </tbody>
                </table>
                <div data-role="controlgroup" data-type="horizontal" data-theme="a">
                    <a href="#" data-role="button" data-theme="a"><label id="labelTM">N.A.</label></a>
                    <a href="#" data-role="button" data-theme="a"><label id="labelNbtapis">N.A.</label></a>
                    <a href="#" data-role="button" data-theme="a"><label id="label50">N.A.</label></a>
                    <a href="#" data-role="button" data-theme="a"><label id="label30">N.A.</label></a>
                    <a href="#" data-role="button" data-theme="a"><label id="label20">N.A.</label></a>
                    <a href="#" data-role="button" data-theme="a"><label id="labelHeure">N.A.</label></a>
                </div>
                <div id="labelDateTournoi"></div>
                </center>
                <div id="msgRetour"></div>
            </div>
        </div>
        <!-- /content -->

        <!-- footer -->
        <div data-role="footer" data-position="fixed">
            <a data-role="button" data-icon="search" data-iconpos="notext" class="ui-btn-left" href="javascript:window.location = ('./api_page_faq.html?mili='+$.functionsLib.getMilise());">FAQ</a>
            <h1 id="id_affichageUser">User</h1>
            <a data-role="button" data-icon="delete" data-iconpos="notext" class="ui-btn-right" href="javascript:window.location = ('./api_page_sortie.html?mili='+$.functionsLib.getMilise());">Logout</a>
        </div>
        <!-- /footer -->

    </div>
    <!-- /page -->
</body>
</html>